<template>
	<view class="uni-container">
		<!-- <uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="vip" label="vip价格">
				<uni-easyinput placeholder="vip价格,100等于1元" type="number" v-model="formData.vip"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="svip" label="svip价格">
				<uni-easyinput placeholder="svip价格,100等于1元" type="number" v-model="formData.svip"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="vip_price" label="vip折扣">
				<uni-easyinput placeholder="vip购买商品时的折扣,90等于9折" type="number"
					v-model="formData.vip_price"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="svip_price" label="svip折扣">
				<uni-easyinput placeholder="vip购买商品时的折扣,95等于95折" type="number"
					v-model="formData.svip_price"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms> -->
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="user_grade_name" label="会员等级">
				<uni-easyinput placeholder="请输入会员等级名称" type="string" v-model="formData.user_grade_name"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="price" label="所需积分">
				<uni-easyinput placeholder="升级会员所需积分" type="number" v-model="formData.price"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="one_pro" label="一级分销">
				<uni-easyinput placeholder="请输入一级分销比例" type="number" v-model="formData.one_pro"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="two_pro" label="二级分销">
				<uni-easyinput placeholder="请输入二级分销比例" type="number" v-model="formData.two_pro"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="order_subsidies" label="订单补贴">
				<uni-easyinput placeholder="订单完成补贴比例" type="number" v-model="formData.order_subsidies"></uni-easyinput>
			</uni-forms-item>
		</uni-forms>
		<view class="uni-button-group">
			<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
			<navigator open-type="navigateBack" style="margin-left: 15px;">
				<button class="uni-button" style="width: 100px;">返回</button>
			</navigator>
		</view>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/jlg-parameter.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'jlg-parameter';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"user_grade_name": null,
				"price": null,
				"user_grade":null,
				"one_pro": null,
				"two_pro": null,
				"order_subsidies": null
			}
			return {
				formData,
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				let res=this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					if(res.result.code==0){
						uni.showToast({
							title: '新增成功'
						})
						this.getOpenerEventChannel().emit('refreshData')
						setTimeout(() => uni.navigateBack(), 500)
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
			
		}
	}
</script>